import os
import shutil
from pathlib import Path

import gradio as gr

IMG_PATH = "./test_img"


def a(img):
    print(img)
    shutil.copy(img, "./test_img")
    paths = sorted(Path(IMG_PATH).rglob('*.jpeg'))

    return img, gr.Dataset.update(samples=[[path.as_posix()] for path in paths])


with gr.Blocks() as demo:
    # 人脸识别 录入
    with gr.Row():
        with gr.Column():
            with gr.Row():
                input_img = gr.Image(image_mode="RGB", source="upload", type="filepath", label="人脸录入")
            with gr.Row():
                btn = gr.Button(value="录入")
            with gr.Row():
                paths = sorted(Path(IMG_PATH).rglob('*.jpeg'))
                example_images = gr.Dataset(components=[input_img], samples=[[path.as_posix()] for path in paths])

        with gr.Column():
            with gr.Row():
                output_ = gr.Image(image_mode="RGB", source="upload", type="pil", label="录入图片")

    btn.click(fn=a, inputs=[input_img], outputs=[output_, example_images])
    demo.launch(inbrowser=True)
